#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id == -1)
    {
        perror("fork");
        exit(1);
    }
    else if(id == 0)
    {
        printf("pid:%d,child process running..\n",getpid());
        int ret = execl("/usr/bin/ls","ls","-a","-l","--color=auto",NULL);
        char* argv[]={
    (char*)"ls",
    (char*)"-a",
    (char*)"-l",
    (char*)"--color=auto",
    NULL  
    };
    execvp("ls",argv);
    extern char** environ;
    execle("./mybin", "mybin", NULL, environ); //自定义环境变量
    putenv((char*)"MYENV=4443332211"); //将指定环境变量导入到系统中 environ指向的环境变量表
    execle("./mybin", "mybin", NULL, environ); //实际上，默认环境变量你不传，子进程也能获取
        if(ret == -1)
        {
            printf("process exec fail\n");
            exit(1);
        }
       	printf("pid:%d,child process done..\n",getpid());
        return 0;
    }
    
    int status;
    pid_t ret = waitpid(id, &status, 0);
    if(ret == -1)
    {
        perror("waitpid");
        return 1;
    }
    else
    {
        printf("wait success: exit code: %d, sig: %d\n", (status>>8)&0xFF, status & 0x7F);
    }
    
    return 0;
}
